# 重新下载图片目录下下载失败的文件，文件格式text
import os
import time
from concurrent import futures

import common
import porn
from common import DownTool
from common import FileTool
from common import LoggerTool

logger = LoggerTool.get_logger(__name__)
# 用户目录
user_dir = os.path.expanduser('~') + os.sep
DOWN_PATH = user_dir + 'Pictures/Camera Roll/PORN/'
path = '\\自拍达人原创申请_JH\\2022-07\\'

if __name__ == '__main__':
    file_list = FileTool.get_file_list('text', DOWN_PATH)
    for item in file_list:
        split = item.split('un_done')
        down_file_path = split[0]
        down_urls = []
        os.chdir(down_file_path)
        with open(item, 'r', encoding='utf-8') as file_obj:
            for num, value in enumerate(file_obj, 1):
                # logger.info('read file........
                line = value.strip('\n')
                if line == '':
                    logger.info('当前行为空：%i line' % num)
                    continue
                pic_url = line.split('图片链接 -')
                url_split = pic_url[1].split('.live')
                if len(url_split) == 1:
                    url_split = str(pic_url[1]).split('.com')
                img_url = porn.pre_url + url_split[1]
                image_name = img_url.split("/")[-1]
                if not os.path.exists(image_name):
                    logger.info('图片已存在，%s,%s;url: %s' % (item, image_name, img_url))
                else:
                    down_urls.append(img_url)

        if len(down_urls) > 0:

            fs = []
            start = time.time()
            with futures.ThreadPoolExecutor(max_workers=5 if len(down_urls) > 5 else len(down_urls),
                                            thread_name_prefix="down-thread") as executor:
                for value1 in down_urls:
                    submit = executor.submit(DownTool.down_img, value1)
                    # submit完成之后的回调函数
                    submit.add_done_callback(common.executor_callback)
                    fs.append(submit)
            futures.wait(fs, timeout=15)
        os.remove(item)
        logger.info('第 %i 行： %s 下载完毕；用时：%i ' % (num, line, time.time() - start))
